package com.hig.questions.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hig.questions.domain.ExamType;
import com.hig.questions.excel.ExamQuestionsPropertyExcel;
import com.hig.questions.service.IExamQuestionsBankService;
import com.hig.questions.service.IExamTypeService;
import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.hig.questions.domain.ExamQuestionsProperty;
import com.hig.questions.service.IExamQuestionsPropertyService;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 题目属性Controller
 * 
 * @author qnsdt
 * @date 2022-12-28
 */
@RestController
@RequestMapping("/questions/questionsproperty")
public class ExamQuestionsPropertyController extends BaseController
{
    @Autowired
    private IExamQuestionsPropertyService examQuestionsPropertyService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IExamTypeService examTypeService;



    /**
     * 查询题目属性列表
     */
  //  @PreAuthorize("@ss.hasPermi('questions:questionsproperty:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamQuestionsProperty examQuestionsProperty)
    {

        Long examType = examQuestionsProperty.getExamType();

        if (ObjectUtil.isNotNull(examType)){
            List<ExamType> allSubTypes = examTypeService.getAllSubTypes(examType);
            if (CollectionUtil.isNotEmpty(allSubTypes)){
                List<String> collect = allSubTypes.stream().map(a -> a.getBankCode()).collect(Collectors.toList());
                examQuestionsProperty.setBankCodes(collect);
                examQuestionsProperty.setBankCode(null);
            }else {
                return getDataTable(new ArrayList<>());
            }

        }
        startPage();
        List<ExamQuestionsProperty> list = examQuestionsPropertyService.selectExamQuestionsPropertyList(examQuestionsProperty);
        return getDataTable(list);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ExamQuestionsPropertyExcel> util = new ExcelUtil<ExamQuestionsPropertyExcel>(ExamQuestionsPropertyExcel.class);
        ArrayList<ExamQuestionsPropertyExcel> array = new ArrayList<>();
         util.exportExcel(response,array,"试题模版");
    }

    /**
     * 试题导入
     */
    @Log(title = "试题导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('questions:questionsproperty:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,String bankCode) throws Exception
    {
        if (ObjectUtil.isNull(bankCode)||"undefined".equals(bankCode)||"null".equals(bankCode) ){
            throw new RuntimeException("请选择题库后上传");
        }
        ExcelUtil<ExamQuestionsPropertyExcel> util = new ExcelUtil<>(ExamQuestionsPropertyExcel.class);
        List<ExamQuestionsPropertyExcel> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = examQuestionsPropertyService.importQuestions(userList, updateSupport, operName,bankCode);
        return AjaxResult.success(message);
    }
    /**
     * 导出题目属性列表
     */
    @PreAuthorize("@ss.hasPermi('questions:questionsproperty:export')")
    @Log(title = "题目属性", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ExamQuestionsProperty examQuestionsProperty)
    {
        List<ExamQuestionsProperty> list = examQuestionsPropertyService.selectExamQuestionsPropertyList(examQuestionsProperty);
        ExcelUtil<ExamQuestionsProperty> util = new ExcelUtil<ExamQuestionsProperty>(ExamQuestionsProperty.class);
        return util.exportExcel(list, "题目属性数据");
    }

    /**
     * 获取题目属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('questions:questionsproperty:query')")
    @GetMapping(value = "/{questionsCode}")
    public AjaxResult getInfo(@PathVariable("questionsCode") String questionsCode)
    {
        return AjaxResult.success(examQuestionsPropertyService.selectExamQuestionsPropertyById(questionsCode));
    }

    /**
     * 新增题目属性
     */
    @PreAuthorize("@ss.hasPermi('questions:questionsproperty:add')")
    @Log(title = "题目属性", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody ExamQuestionsProperty examQuestionsProperty)
    {
    	LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
    	examQuestionsProperty.setCreateBy(loginUser.getUser().getUserName());
    	examQuestionsProperty.setCreateDept(loginUser.getUser().getDept().getDeptId());

        return toAjax(examQuestionsPropertyService.insertExamQuestionsProperty(examQuestionsProperty));
    }

    /**
     * 修改题目属性
     */
    @PreAuthorize("@ss.hasPermi('questions:questionsproperty:edit')")
    @Log(title = "题目属性", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody ExamQuestionsProperty examQuestionsProperty)
    {
        return toAjax(examQuestionsPropertyService.updateExamQuestionsProperty(examQuestionsProperty));
    }

    /**
     * 删除题目属性
     */
    @PreAuthorize("@ss.hasPermi('questions:questionsproperty:remove')")
    @Log(title = "题目属性", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{questionsCodes}")
    public AjaxResult remove(@PathVariable String[] questionsCodes)
    {
        return toAjax(examQuestionsPropertyService.deleteExamQuestionsPropertyByIds(questionsCodes));
    }
}
